import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
import jieba
from sklearn.preprocessing import StandardScaler
from Tools import readbunchobj, metrics_result

# 加载保存的模型和TfidfVectorizer
rf_clf = joblib.load('RF_model.joblib')
vectorizer = joblib.load('../vectorizer.joblib')

# 导入训练集
trainpath = "../train_word_bag/tf_idf_space.dat"
train_set = readbunchobj(trainpath)

# 导入测试集
testpath = "../test_word_bag/test_tf_idf_space.dat"
test_set = readbunchobj(testpath)

# 对数据进行标准化或归一化
scaler = StandardScaler(with_mean=False)
train_set.tdm = scaler.fit_transform(train_set.tdm)
test_set.tdm = scaler.transform(test_set.tdm)

# 预测分类结果
print("USE: 随机森林模型 预测分类结果")
rf_predicted = rf_clf.predict(test_set.tdm)
rf_total = len(rf_predicted)
print("结束")

print(rf_predicted)
print(rf_total)

# 性能评估
print("\n随机森林模型：")
metrics_result(test_set.label, rf_predicted, rf_total)

